package cn.com.open.itembank.mapper;

import cn.com.open.itembank.dto.ChapterModel;
import cn.com.open.itembank.entity.Chapter;
import cn.com.open.itembank.entity.CourseItemBank;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created by lucky on 2017/7/17.
 */
@Mapper
public interface ChapterMapper {

    /**
     * 根据章节id获取章节信息.
     *
     * @param id 章节id
     * @return 章节信息 by id
     */
    @Select("SELECT * FROM Chapter WHERE ChapterID = #{id} AND Status <> -1")
    Chapter getById(@Param("id") String id);

    /**
     * 修改时是否重名
     *
     * @param itemBankId  题库ID
     * @param id          章节id
     * @param pid         父章节id
     * @param chapterName 章节名称
     * @return the int
     */
    @Select("SELECT COUNT(*) FROM Chapter WHERE ChapterName = #{chapterName} AND ParentChapterID = #{pid} AND CourseID=#{itemBankId} AND ChapterID  <> #{id}")
    int isExistChapter(@Param("itemBankId") String itemBankId, @Param("id") String id, @Param("pid") String pid, @Param("chapterName") String chapterName);

    /**
     * 根据父节点ID和章节名称获取章节个数.
     *
     * @param itemBankId      题库ID
     * @param parentChapterID 父章节id
     * @param chapterName     章节名称
     * @return the int
     */
    @Select("SELECT COUNT(*) FROM Chapter WHERE ParentChapterID = #{pid} AND ChapterName = #{name} AND CourseID=#{itemBankId} AND Status <> -1")
    int getCountByPidAndName(@Param("itemBankId") String itemBankId, @Param("pid") String parentChapterID, @Param("name") String chapterName);

    /**
     * 根据父节点ID获取排序最大序号.
     *
     * @param parentChapterID 父章节id
     * @return the int
     */
    @Select("SELECT ISNULL(MAX(Orders),0) MaxOrder FROM Chapter WHERE ParentChapterID= #{pid}")
    int getMaxOrdersByPid(@Param("pid") String parentChapterID);

    //region 新增修改删除章节信息

    /**
     * 新增章节信息.
     *
     * @param model 章节信息
     * @return the int
     */
    @Insert("INSERT INTO Chapter (ChapterID,ParentChapterID,ChapterName,CreateTime,ChapterType,Comment,CourseID,Creater,Depth,Orders,Status) VALUES (#{chapterID},#{parentChapterID},#{chapterName},#{createTime},#{chapterType},#{comment},#{courseID},#{creater},#{depth},#{orders},#{status})")
    int insert(Chapter model);

    /**
     * 修改章节名称.
     *
     * @param id          章节id
     * @param chapterName 章节名称
     * @return the int
     */
    @Update("UPDATE Chapter SET ChapterName=#{chapterName} WHERE ChapterID=#{id}")
    int update(@Param("id") String id, @Param("chapterName") String chapterName);

    /**
     * 删除题库下全部章节信息.
     *
     * @param itemBankId 题库ID
     * @return the int
     */
    @Delete("UPDATE Chapter set Status = -1 WHERE CourseID=#{itemBankId}")
    int deleteByItemBankId(@Param("itemBankId") String itemBankId);

    /**
     * 获取章节列表
     *
     * @param itemBankId
     * @return
     */
    @Select("select * from Chapter where CourseID=#{itemBankId} and Status = 0")
    List<Chapter> getChapterListByItemBnakId(@Param("itemBankId") String itemBankId);

    /**
     * 根据章节id删除章节
     *
     * @param id
     * @return
     */
    @Update("UPDATE Chapter set Status = -1 WHERE ChapterID=#{id}")
    int deleteById(@Param("id") String id);

    /**
     * 查询此节点下的所有子节点
     *
     * @param chapterId
     * @return
     */
    @Select("select ChapterID from Chapter where ParentChapterID = #{chapterId}")
    List<String> getParentChapterIDById(@Param("chapterId") String chapterId);

    /**
     * 查询章节是否正在被使用
     *
     * @param chapterIds
     * @return
     */
    @Select("select count(1) from QuestionChapterInfo where ChapterID = #{chapterIds}")
    int getQuestionChapterInfo(@Param("chapterIds") String chapterIds);

    /**
     * 获取章节总数
     *
     * @return
     */
    @Select("select count(1) from Chapter")
    int getChapterCount();

    /**
     * 验证题库是否存在
     *
     * @param itemBankId
     * @return
     */
    @Select("select * from CourseItemBank where ItemBankId = #{itemBankId}")
    CourseItemBank getCourseItemBank(@Param("itemBankId") String itemBankId);

    /**
     * 获取章节集合（策略用）
     *
     * @param chapterId
     * @param courseID
     * @param depth
     * @param orders
     * @param i
     * @return
     */
    @Select("select * from Chapter where ChapterID = #{chapterId} and CourseID = #{courseID} and Depth = #{depth} and Orders = #{i}")
    List<Chapter> getChaptcerList(@Param("chapterId") String chapterId, @Param("courseID") String courseID, @Param("depth") Integer depth, @Param("orders") Integer orders, @Param("i") int i);

    /**
     * 拖拽本层
     *
     * @param newChapter
     */
    @Update("UPDATE Chapter SET ChapterName=#{chapterName},ParentChapterID=#{parentChapterID},ChapterType=#{chapterType},Comment=#{comment},CourseID=#{" +
            "courseID},Orders=#{orders},Status=#{status},Creater=#{creater},CreateTime=#{createTime},Depth=#{depth} WHERE ChapterID=#{chapterID}")
    void updateChapter(Chapter newChapter);

    /**
     * 根据章节id和pid获取章节
     *
     * @param chapterId
     * @param parentId
     * @return
     */
    @Select("select * from Chapter where ChapterID=#{chapterId} AND ParentChapterID=#{parentId} and Status <> -1")
    Chapter getChapterByIdAndPid(@Param("chapterId") String chapterId, @Param("parentId") String parentId);

    /**
     * 查询本章节得兄弟节点得最高层次
     *
     * @param depth
     * @return
     */
    @Select("select TOP 1 * from Chapter where Depth=#{depth} AND Orders=(select max(Orders) from Chapter where Depth=#{depth})")
    Chapter getChapterByIdAndDepth(@Param("depth") Integer depth);

    /**
     * @param i
     * @param depth
     * @return
     */
    @Select("select * from Chapter where Orders=#{i} AND Depth = #{depth} AND ParentChapterID = #{pid} and Status <> -1")
    Chapter getChapter(@Param("i") int i, @Param("depth") Integer depth, @Param("pid") String pid);

    /**
     * 获取章节层级
     *
     * @param itemBankId
     */
    @Select("select * from Chapter where CourseID =#{itemBankId} AND Status <> -1 order by Depth,Orders Asc")
    List<Chapter> getChapterByOrder(@Param("itemBankId") String itemBankId);

    /**
     * 查询第一条
     *
     * @param parentId
     * @return
     */
    @Select("select TOP 1 * from Chapter where ParentChapterID = #{parentId} and Status <> -1")
    Chapter getChapterListByParentId(@Param("parentId") String parentId);


    /**
     * 根据父章节id获取章节集合
     *
     * @param parentId
     */
    @Select("select * from Chapter where ParentChapterID = #{parentId} order by Orders")
    List<Chapter> getChapterListByParentIdOrder(@Param("parentId") String parentId);

    /**
     * 查询父章节的深度（删除）
     * @param depth
     * @return
     */
    @Select("select * from Chapter where Depth = #{depth}")
    List<Chapter> getDepthById(@Param("depth") Integer depth);


    /**
     * 根据order和depth获取章节（删除）
     * @param i
     * @param depth
     * @return
     */
    @Select("select * from Chapter where Orders=#{i} AND Depth = #{depth}")
    Chapter getChapterByOrderAndDepth(@Param("i") int i, @Param("depth") Integer depth);

    /**
     * 根据章节id查询父章节
     * @param chapterID
     */
    @Select("select * from Chapter where ParentChapterID = #{chapterID}")
    List<ChapterModel> getChapterModel(@Param("chapterID") String chapterID);


    /**
     *
     * @param parentChapterID
     * @return
     */
    @Select("select * from Chapter where ParentChapterID = #{parentChapterID} AND Status <> -1 order by Depth")
    List<Chapter> getParentChapter(@Param("parentChapterID") String parentChapterID);

    /**
     * 获取章节根据章节集合和章节id
     * @param chapterId
     */
    @Select("select * from Chapter where ParentChapterID in (#{chapterId}) AND ChapterId != #{chapterId} AND Status <> -1 order by Depth")
    List<Chapter> getChapterByChapterList(@Param("chapterId") String chapterId);

    /**
     * 删除后排序
     * @param chapId
     */
    @Update("update Chapter set Orders = Orders-1 where ChapterID = #{chapId}")
    void updateChapterOrder(@Param("chapId") String chapId);

    //endregion
}
